/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.tms.service;

import java.util.List;

import com.jeeplus.modules.ap.entity.BillAdd;
import com.jeeplus.modules.ap.service.BillAddService;
import com.jeeplus.modules.infor.service.GoodsService;
import com.jeeplus.modules.infor.service.LocationService;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.tms.entity.*;
import com.jeeplus.modules.wms.entity.Inventory;
import com.jeeplus.modules.wms.entity.Outbound;
import com.jeeplus.modules.wms.entity.PickDetailed;
import com.jeeplus.modules.wms.service.InventoryService;
import com.jeeplus.modules.wms.service.OutboundService;
import com.jeeplus.modules.wms.service.PickDetailedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.tms.mapper.TmsWaybillMapper;
import com.jeeplus.modules.tms.mapper.TmsAdditionalChargeMapper;
import com.jeeplus.modules.tms.mapper.TmsMaterialsInformationMapper;

/**
 * 运单Service
 * @author 李超辉
 * @version 2017-12-05
 */
@Service
@Transactional(readOnly = true)
public class TmsWaybillService extends CrudService<TmsWaybillMapper, TmsWaybill> {

	@Autowired
	private TmsAdditionalChargeMapper tmsAdditionalChargeMapper;
	@Autowired
	private TmsMaterialsInformationMapper tmsMaterialsInformationMapper;
	@Autowired
	private TransportPlanningService service;
	@Autowired
	private OutboundService outboundService;
	@Autowired
	private BillAddService billAddService;
	@Autowired
	private JdbcTemplate jdbc;
	public TmsWaybill get(String id) {
		TmsWaybill tmsWaybill = super.get(id);
		tmsWaybill.setTmsAdditionalChargeList(tmsAdditionalChargeMapper.findList(new TmsAdditionalCharge(tmsWaybill)));
		tmsWaybill.setTmsMaterialsInformationList(tmsMaterialsInformationMapper.findList(new TmsMaterialsInformation(tmsWaybill)));
		return tmsWaybill;
	}
	@Transactional(readOnly = false)
	public  String qxpz(String i){
		TransportPlanning t=service.get(i);
		t.setWf(t.getWeight());
		t.setStatus("已创建");
		t.setPlanStatus("未计划");
		for(MaterialInformations m:t.getMaterialInformationsList()) {
			m.setVehicleQuantity("0");
			m.setRemain(m.getGross());
		}
		TmsWaybill tms=new TmsWaybill();
		tms.setShipmentNumbers(t.getShipmentNumbers());
		List<TmsWaybill> l=findList(tms);
		boolean bol=true;
		for(TmsWaybill w:l){
			if(w.getBillStatus().equals("已生成合同")||w.getBillStatus().equals("在途")||w.getBillStatus().equals("已签收")){
				return "ht";
			}
			Outbound outbound=new Outbound();
			outbound.setExternalOrder(w.getCode());
				List<Outbound> o=	outboundService.findList(outbound);
			if(o!=null&&o.size()>0){
				for(Outbound oo:o){
				if(!oo.getStatus().equals("已创建")){
					bol=false;
					return "false";
				}}
		}
if(bol){

	for(TmsWaybill w1:l){
		Outbound outboun=new Outbound();
		outboun.setExternalOrder(w.getCode());
		List<Outbound> o2=	outboundService.findList(outboun);
		if(o2!=null&&o2.size()>0){
			for(Outbound oo:o2){
				outboundService.delete(oo);
				}
		}
		delete(w1);
	}
}}
			service.save(t);
		return "";
	}
	public List<TmsWaybill> findList(TmsWaybill tmsWaybill) {
		return super.findList(tmsWaybill);
	}
	
	public Page<TmsWaybill> findPage(Page<TmsWaybill> page, TmsWaybill tmsWaybill) {
		return super.findPage(page, tmsWaybill);
	}
	
	@Transactional(readOnly = false)
	public void save(TmsWaybill tmsWaybill) {
		super.save(tmsWaybill);
		for (TmsAdditionalCharge tmsAdditionalCharge : tmsWaybill.getTmsAdditionalChargeList()){
			if (tmsAdditionalCharge.getId() == null){
				continue;
			}
			if (TmsAdditionalCharge.DEL_FLAG_NORMAL.equals(tmsAdditionalCharge.getDelFlag())){
				if (StringUtils.isBlank(tmsAdditionalCharge.getId())){
					tmsAdditionalCharge.setWaybill(tmsWaybill);
					tmsAdditionalCharge.preInsert();
					tmsAdditionalChargeMapper.insert(tmsAdditionalCharge);
				}else{
					tmsAdditionalCharge.preUpdate();
					tmsAdditionalChargeMapper.update(tmsAdditionalCharge);
				}
			}else{
				tmsAdditionalChargeMapper.delete(tmsAdditionalCharge);
			}
		}
		for (TmsMaterialsInformation tmsMaterialsInformation : tmsWaybill.getTmsMaterialsInformationList()){
			if (tmsMaterialsInformation.getId() == null){
				continue;
			}
			if (TmsMaterialsInformation.DEL_FLAG_NORMAL.equals(tmsMaterialsInformation.getDelFlag())){
				if (StringUtils.isBlank(tmsMaterialsInformation.getId())){
					tmsMaterialsInformation.setWaybill(tmsWaybill);
					tmsMaterialsInformation.preInsert();
					tmsMaterialsInformationMapper.insert(tmsMaterialsInformation);
				}else{
					tmsMaterialsInformation.preUpdate();
					tmsMaterialsInformationMapper.update(tmsMaterialsInformation);
				}
			}else{
				tmsMaterialsInformationMapper.delete(tmsMaterialsInformation);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(TmsWaybill tmsWaybill) {
		super.delete(tmsWaybill);
		tmsAdditionalChargeMapper.delete(new TmsAdditionalCharge(tmsWaybill));
		tmsMaterialsInformationMapper.delete(new TmsMaterialsInformation(tmsWaybill));
	}
	
}